﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Industry.Framework.Common.Helper;

namespace Onlyle.Business
{
    public class PagerHelper
    {
        private int _pageIndex = 1;
        private int _pageSize = 10;
        private int _counts = 0;

        private int PageCount
        {
            get
            {
                if (Counts % pageSize == 0)
                {
                    return Counts / pageSize;
                }
                else
                {
                    return (int)(Counts / pageSize) + 1;
                }
            }
        }

        public int pageIndex
        {
            get { return _pageIndex; }
            set { _pageIndex = value; }
        }

        public int pageSize
        {
            get { return _pageSize; }
            set { _pageSize = value; }
        }

        public int Counts
        {
            get { return _counts; }
            set { _counts = value; }
        }
        /// <summary>
        /// 分页条
        /// </summary>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageCount">总页数</param>
        /// <param name="Url">点击页码跳转到Url页面</param>
        /// <returns></returns>
        public static string Pageer(string pageIndex, string pageCount, int PageSzie, string Url)
        {
            //if (Url.ToLower() == "index?")
            //{
            //    Url = "gaoxiao/meinvfuli?";
            //}
            string html = string.Empty;
            string html1 = string.Empty;
            int ipageIndex = 1;
            int ipageCount = 1;
            if (StringHelper.IsNumberId(pageIndex))
            {
                ipageIndex = Convert.ToInt32(pageIndex);
            }
            if (StringHelper.IsNumberId(pageCount))
            {
                ipageCount = Convert.ToInt32(pageCount);
                ipageCount = ipageCount % PageSzie > 0 ? (ipageCount / PageSzie) + 1 : ipageCount / PageSzie;
            }

            if (ipageCount <= PageSzie)
            {
                for (int i = 1; i < ipageCount + 1; i++)
                {
                    if (i == ipageIndex)
                    {
                        html1 += "<li  class=\"active\"><a href=\"javascript:void(0);\">" + ipageIndex + "</a></li>";
                    }
                    else
                    {
                        html1 += "<li><a href=\"" + Url + "&p=" + i + "\">" + i + "</a></li>";
                    }
                }
            }
            else if (ipageIndex <= 4)
            {
                for (int i = 1; i < 11; i++)
                {
                    if (i == ipageIndex)
                    {
                        html1 += "<li  class=\"active\"><a href=\"javascript:void(0);\">" + i + "</a></li>";
                    }
                    else
                    {
                        html1 += "<li><a href=\"" + Url + "&p=" + i + "\">" + i + "</a></li>";
                    }
                }
            }
            else if (ipageIndex > ipageCount - 5)
            {
                for (int i = ipageIndex - (ipageIndex - (ipageCount - 5) + 4); i < ipageCount + 1; i++)
                {
                    if (i == ipageIndex)
                    {
                        html1 += "<li  class=\"active\"><a href=\"javascript:void(0);\">" + i + "</a></li>";
                    }
                    else
                    {
                        html1 += "<li><a href=\"" + Url + "&p=" + i + "\">" + i + "</a></li>";
                    }
                }
            }
            else
            {
                for (int i = ipageIndex - 4; i < ipageIndex + 6; i++)
                {
                    if (i == ipageIndex)
                    {
                        html1 += "<li  class=\"active\"><a href=\"javascript:void(0);\">" + i + "</a></li>";
                    }
                    else
                    {
                        html1 += "<li><a href=\"" + Url + "&p=" + i + "\">" + i + "</a></li>";
                    }
                }
            }
            html = " <div class=\"pagination\"><ul>";
            if (ipageIndex == 0 || ipageIndex == 1)
            {
                html += "<li><a href=\"javascript:void(0);\">&larr;</a></li>";
            }
            else
            {
                html += "<li><a href=\"" + Url + "&p=" + (ipageIndex - 1) + "\">&larr;</a></li>";
            }
            html += html1;
            if (ipageIndex >= ipageCount)
            {
                html += "<li><a href=\"javascript:void(0);\">&rarr;</a></li>";
            }
            else
            {
                html += "<li><a href=\"" + Url + "&p=" + (ipageIndex + 1) + "\">&rarr;</a></li>";
            }
            return html + "</ul></div>";
        }


    }
}
